package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.Binding;
import com.ruoyi.business.domain.ClassInfo;
import com.ruoyi.business.domain.School;
import com.ruoyi.business.mapper.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.domain.User;
import com.ruoyi.business.service.IUserService;
import org.springframework.util.ObjectUtils;

/**
 * 用户Service业务层处理
 * 
 * @author lcz
 * @date 2025-02-21
 */
@Service
public class UserServiceImpl implements IUserService 
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Autowired
    private BindingMapper bindingMapper;

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public User selectUserById(Long id)
    {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param user 用户
     * @return 用户
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setCreateTime(DateUtils.getNowDate());
        if (!ObjectUtils.isEmpty(sysUser.getSchoolId())){
            School school = schoolMapper.selectSchoolById(sysUser.getSchoolId());
            user.setSchoolId(sysUser.getSchoolId());
            user.setSchoolName(ObjectUtils.isEmpty(school) ? "" : school.getSchoolName());
        }
        if (!ObjectUtils.isEmpty(user.getClassId())){
            ClassInfo classInfo = classInfoMapper.selectClassInfoById(user.getClassId());
            user.setClassId(classInfo.getId());
            user.setClassName(ObjectUtils.isEmpty(classInfo) ? "" : classInfo.getClassName());
        }
        if (!ObjectUtils.isEmpty(user.getfId())){
            User parent = userMapper.selectUserById(user.getfId());
            if (!ObjectUtils.isEmpty(parent)){
                user.setFName(parent.getUserName());
            }
        }
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户
     * 
     * @param user 用户
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        user.setUpdateTime(DateUtils.getNowDate());
        if (!ObjectUtils.isEmpty(user.getSchoolId())){
            School school = schoolMapper.selectSchoolById(user.getSchoolId());
            user.setSchoolName(ObjectUtils.isEmpty(school) ? "" : school.getSchoolName());
        }
        if (!ObjectUtils.isEmpty(user.getClassId())){
            ClassInfo classInfo = classInfoMapper.selectClassInfoById(user.getClassId());
            user.setClassName(ObjectUtils.isEmpty(classInfo) ? "" : classInfo.getClassName());
        }
        if (ObjectUtils.isEmpty(user.getPassword())){
            user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        }
        if (!ObjectUtils.isEmpty(user.getfId())){
            User parent = userMapper.selectUserById(user.getfId());
            if (!ObjectUtils.isEmpty(parent)){
                user.setFName(parent.getUserName());
            }
        }
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids)
    {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id)
    {
        return userMapper.deleteUserById(id);
    }

    @Override
    public int batchInsertUser(List<User> userList) {
        return userMapper.batchInsertUser(userList);
    }

    @Override
    public AjaxResult batchInsertUserAndBinding(List<User> userList) {
        for (User user : userList){

            userMapper.insertUser(user);

            Binding binding = new Binding();
            binding.setUserId(user.getId());
            binding.setAccount(user.getAccount());
            binding.setCode(user.getCode());
            binding.setStudentName(user.getUserName());
            binding.setSchoolId(user.getSchoolId());
            binding.setCreateTime(DateUtils.getNowDate());
            bindingMapper.insertBinding(binding);
        }
        return AjaxResult.success();
    }
}
